/*************************************************************************
	> File Name: oj-304_knight_cout.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2020年11月18日 星期三 18时23分42秒
 ************************************************************************/

#include <iostream>
#include <queue>
using namespace std;

struct node {
    int x, y, step;
};

int n, m;
int dir[8][2] = {
    1, 2,
    1, -2,
    -1, 2,
    -1, -2,
    2, 1,
    2, -1,
    -2, 1,
    -2, -1
};
char mmap[200][200];

int main() {
    cin >> m >> n;
    queue<node> que;
    for (int i = 5; i < n + 5; ++i) {
        for (int j = 5; j < m + 5; ++j) {
            cin >> mmap[i][j];
            if (mmap[i][j] == 'K') {
                que.push((node){i, j, 0});
            }
        }
    }
    while (!que.empty()) {
        node temp = que.front();
        que.pop();
        for (int i = 0; i < 8; ++i) {
            int x = temp.x + dir[i][0];
            int y = temp.y + dir[i][1];
            if (mmap[x][y] == 'H') {
                cout << temp.step + 1 << endl;
                return 0;
            }
            if (mmap[x][y] == '.') {
                mmap[x][y] = 0;
                que.push((node){x, y, temp.step + 1});
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
